#!/bin/sh
cd tests

rm -rf tests.log

echo nameserver 8.8.4.4 >> /etc/resolv.conf

# known good tests
MQTT_TESTS="\
mqtt5_topic_skip_rules_prefix \
mqtt5_topic_get_segment_count \
mqtt5_shared_subscription_validation \
mqtt5_inbound_topic_alias_register_failure \
mqtt5_inbound_topic_alias_resolve_success \
mqtt5_inbound_topic_alias_resolve_failure \
mqtt5_inbound_topic_alias_reset \
mqtt5_outbound_topic_alias_disabled_resolve_success \
mqtt5_outbound_topic_alias_disabled_resolve_failure \
mqtt5_outbound_topic_alias_user_resolve_failure_zero_alias \
mqtt5_outbound_topic_alias_user_resolve_failure_too_big_alias \
mqtt5_outbound_topic_alias_user_resolve_success \
mqtt5_outbound_topic_alias_user_reset \
mqtt5_outbound_topic_alias_lru_zero_size \
mqtt5_outbound_topic_alias_lru_a_ar \
mqtt5_outbound_topic_alias_lru_b_a_br \
mqtt5_outbound_topic_alias_lru_a_b_ar_br \
mqtt5_outbound_topic_alias_lru_a_b_c_br_cr_br_cr_a \
mqtt5_outbound_topic_alias_lru_a_b_c_a_cr_b \
mqtt5_outbound_topic_alias_lru_a_b_reset_a_b \
mqtt5_publish_operation_new_set_no_optional \
mqtt5_publish_operation_new_set_all \
mqtt5_publish_operation_new_failure_packet_id \
mqtt5_subscribe_operation_new_set_no_optional \
mqtt5_subscribe_operation_new_set_all \
mqtt5_unsubscribe_operation_new_set_all \
mqtt5_connect_storage_new_set_no_optional \
mqtt5_connect_storage_new_set_all \
mqtt5_connack_storage_new_set_no_optional \
mqtt5_connack_storage_new_set_all \
mqtt5_disconnect_storage_new_set_no_optional \
mqtt5_disconnect_storage_new_set_all \
mqtt5_suback_storage_new_set_no_optional \
mqtt5_suback_storage_new_set_all \
mqtt5_unsuback_storage_new_set_no_optional \
mqtt5_unsuback_storage_new_set_all \
mqtt5_puback_storage_new_set_all \
mqtt5_publish_storage_new_set_all \
mqtt5_operation_disconnect_validation_failure_server_reference \
mqtt5_operation_disconnect_validation_failure_bad_reason_code \
mqtt5_operation_disconnect_validation_failure_reason_string_too_long \
mqtt5_operation_disconnect_validation_failure_user_properties_name_too_long \
mqtt5_operation_disconnect_validation_failure_user_properties_value_too_long \
mqtt5_operation_disconnect_validation_failure_user_properties_too_many \
mqtt5_operation_connect_validation_failure_client_id_too_long \
mqtt5_operation_connect_validation_failure_username_too_long \
mqtt5_operation_connect_validation_failure_password_too_long \
mqtt5_operation_connect_validation_failure_receive_maximum_zero \
mqtt5_operation_connect_validation_failure_maximum_packet_size_zero \
mqtt5_operation_connect_validation_failure_will_invalid \
mqtt5_operation_connect_validation_failure_will_payload_too_long \
mqtt5_operation_connect_validation_failure_auth_method_unsupported \
mqtt5_operation_connect_validation_failure_auth_data_unsupported \
mqtt5_operation_connect_validation_failure_request_problem_information_invalid \
mqtt5_operation_connect_validation_failure_request_response_information_invalid \
mqtt5_operation_connect_validation_failure_user_properties_name_too_long \
mqtt5_operation_connect_validation_failure_user_properties_value_too_long \
mqtt5_operation_connect_validation_failure_user_properties_too_many \
mqtt5_operation_subscribe_validation_failure_no_subscriptions \
mqtt5_operation_subscribe_validation_failure_too_many_subscriptions \
mqtt5_operation_subscribe_validation_failure_too_many_subscriptions_for_iot_core \
mqtt5_operation_subscribe_validation_failure_invalid_subscription_identifier \
mqtt5_operation_subscribe_validation_failure_invalid_topic_filter \
mqtt5_operation_subscribe_validation_failure_invalid_topic_filter_for_iot_core \
mqtt5_operation_subscribe_validation_failure_invalid_qos \
mqtt5_operation_subscribe_validation_failure_invalid_retain_type \
mqtt5_operation_subscribe_validation_failure_invalid_no_local \
mqtt5_operation_subscribe_validation_failure_user_properties_name_too_long \
mqtt5_operation_subscribe_validation_failure_user_properties_value_too_long \
mqtt5_operation_subscribe_validation_failure_user_properties_too_many \
mqtt5_operation_unsubscribe_validation_failure_no_topic_filters \
mqtt5_operation_unsubscribe_validation_failure_too_many_topic_filters \
mqtt5_operation_unsubscribe_validation_failure_invalid_topic_filter \
mqtt5_operation_unsubscribe_validation_failure_invalid_topic_filter_for_iot_core \
mqtt5_operation_unsubscribe_validation_failure_user_properties_name_too_long \
mqtt5_operation_unsubscribe_validation_failure_user_properties_value_too_long \
mqtt5_operation_unsubscribe_validation_failure_user_properties_too_many \
mqtt5_operation_publish_validation_failure_invalid_topic \
mqtt5_operation_publish_validation_failure_topic_too_long_for_iot_core \
mqtt5_operation_publish_validation_failure_topic_too_many_slashes_for_iot_core \
mqtt5_operation_publish_validation_failure_no_topic \
mqtt5_operation_publish_validation_failure_invalid_payload_format \
mqtt5_operation_publish_validation_failure_response_topic_too_long \
mqtt5_operation_publish_validation_failure_invalid_response_topic \
mqtt5_operation_publish_validation_failure_correlation_data_too_long \
mqtt5_operation_publish_validation_failure_subscription_identifier_exists \
mqtt5_operation_publish_validation_failure_topic_alias_zero \
mqtt5_operation_publish_validation_failure_user_properties_name_too_long \
mqtt5_operation_publish_validation_failure_user_properties_value_too_long \
mqtt5_operation_publish_validation_failure_user_properties_too_many \
mqtt5_operation_publish_validation_failure_qos0_duplicate_true \
mqtt5_operation_publish_validation_failure_qos0_with_packet_id \
mqtt5_client_options_validation_failure_no_host \
mqtt5_client_options_validation_failure_no_bootstrap \
mqtt5_client_options_validation_failure_no_publish_received \
mqtt5_client_options_validation_failure_invalid_socket_options \
mqtt5_client_options_validation_failure_invalid_connect \
mqtt5_client_options_validation_failure_invalid_keep_alive \
mqtt5_client_options_validation_failure_client_id_too_long_for_iot_core \
mqtt5_operation_subscribe_connection_settings_validation_failure_exceeds_maximum_packet_size \
mqtt5_operation_unsubscribe_connection_settings_validation_failure_exceeds_maximum_packet_size \
mqtt5_operation_publish_connection_settings_validation_failure_exceeds_maximum_packet_size \
mqtt5_operation_publish_connection_settings_validation_failure_exceeds_topic_alias_maximum \
mqtt5_operation_publish_connection_settings_validation_failure_exceeds_maximum_qos \
mqtt5_operation_publish_connection_settings_validation_failure_invalid_retain \
mqtt5_operation_disconnect_connection_settings_validation_failure_exceeds_maximum_packet_size \
mqtt5_operation_disconnect_connection_settings_validation_failure_promote_zero_session_expiry \
mqtt5_client_options_defaults_set \
mqtt5_operation_bind_packet_id_empty_table \
mqtt5_operation_bind_packet_id_multiple_with_existing \
mqtt5_operation_bind_packet_id_multiple_with_wrap_around \
mqtt5_operation_bind_packet_id_full_table \
mqtt5_operation_bind_packet_id_not_valid \
mqtt5_operation_bind_packet_id_already_bound \
mqtt5_operation_processing_nothing_empty_queue \
mqtt5_operation_processing_nothing_mqtt_connect \
mqtt5_operation_processing_nothing_clean_disconnect \
mqtt5_operation_processing_nothing_pending_write_completion_mqtt_connect \
mqtt5_operation_processing_nothing_pending_write_completion_connected \
mqtt5_operation_processing_nothing_pending_write_completion_clean_disconnect \
mqtt5_operation_processing_failure_message_allocation \
mqtt5_operation_processing_failure_message_send \
mqtt5_operation_processing_something_mqtt_connect \
mqtt5_operation_processing_something_clean_disconnect \
mqtt5_operation_processing_something_connected_multi \
mqtt5_operation_processing_something_connected_overflow \
mqtt5_operation_processing_disconnect_fail_all \
mqtt5_operation_processing_disconnect_fail_qos0 \
mqtt5_operation_processing_disconnect_fail_non_qos1 \
mqtt5_operation_processing_reconnect_rejoin_session_fail_all \
mqtt5_operation_processing_reconnect_rejoin_session_fail_qos0 \
mqtt5_operation_processing_reconnect_no_session_fail_all \
mqtt5_operation_processing_reconnect_no_session_fail_qos0 \
mqtt5_operation_processing_reconnect_no_session_fail_non_qos1 \
mqtt5_negotiated_settings_reset_test \
mqtt5_negotiated_settings_apply_connack_test \
mqtt5_negotiated_settings_server_override_test \
mqtt5_vli_size \
mqtt5_vli_success_round_trip \
mqtt5_vli_encode_failures \
mqtt5_vli_decode_failures \
mqtt5_packet_disconnect_round_trip \
mqtt5_packet_pingreq_round_trip \
mqtt5_packet_pingresp_round_trip \
mqtt5_packet_connect_round_trip \
mqtt5_packet_connack_round_trip \
mqtt5_packet_subscribe_round_trip \
mqtt5_packet_suback_round_trip \
mqtt5_packet_unsubscribe_round_trip \
mqtt5_packet_unsuback_round_trip \
mqtt5_packet_publish_round_trip \
mqtt5_packet_puback_round_trip \
mqtt5_packet_encode_connect_no_will \
mqtt5_packet_encode_connect_no_username \
mqtt5_packet_encode_connect_no_password \
mqtt5_packet_encode_connect_will_property_order \
mqtt5_first_byte_reserved_header_check_subscribe \
mqtt5_first_byte_reserved_header_check_unsubscribe \
mqtt5_first_byte_reserved_header_check_disconnect \
mqtt5_client_direct_connect_success \
mqtt5_client_direct_connect_sync_channel_failure \
mqtt5_client_direct_connect_async_channel_failure \
mqtt5_client_websocket_connect_sync_channel_failure \
mqtt5_client_websocket_connect_async_channel_failure \
mqtt5_client_websocket_connect_handshake_failure \
mqtt5_client_direct_connect_connack_refusal \
mqtt5_client_direct_connect_connack_timeout \
mqtt5_client_direct_connect_from_server_disconnect \
mqtt5_client_subscribe_success \
mqtt5_client_unsubscribe_success \
mqtt5_client_sub_pub_unsub_qos0 \
mqtt5_client_sub_pub_unsub_qos1 \
mqtt5_client_ping_sequence \
mqtt5_client_ping_timeout \
mqtt5_client_reconnect_failure_backoff \
mqtt5_client_reconnect_backoff_insufficient_reset \
mqtt5_client_reconnect_backoff_sufficient_reset \
mqtt5_client_subscribe_fail_packet_too_big \
mqtt5_client_disconnect_fail_packet_too_big \
mqtt5_client_flow_control_receive_maximum \
mqtt5_client_publish_timeout \
mqtt5_client_flow_control_iot_core_throughput \
mqtt5_client_flow_control_iot_core_publish_tps \
mqtt5_client_session_resumption_clean_start \
mqtt5_client_session_resumption_post_success \
mqtt5_client_session_resumption_always \
mqtt5_client_receive_qos1_return_puback_test \
mqtt5_client_receive_nonexisting_session_state \
mqtt5_client_receive_assigned_client_id \
mqtt5_client_no_session_after_client_stop \
mqtt5_client_restore_session_on_ping_timeout_reconnect \
mqtt5_client_discard_session_on_server_clean_start \
mqtt5_client_statistics_subscribe \
mqtt5_client_statistics_unsubscribe \
mqtt5_client_statistics_publish_qos0 \
mqtt5_client_statistics_publish_qos1 \
mqtt5_client_statistics_publish_qos1_requeue \
mqtt5_client_puback_ordering \
mqtt5_client_offline_operation_submission_fail_all \
mqtt5_client_offline_operation_submission_fail_qos0 \
mqtt5_client_offline_operation_submission_fail_non_qos1 \
mqtt5_client_offline_operation_submission_then_connect \
mqtt5_client_inbound_alias_success \
mqtt5_client_inbound_alias_failure_disabled \
mqtt5_client_inbound_alias_failure_zero_id \
mqtt5_client_inbound_alias_failure_too_large_id \
mqtt5_client_inbound_alias_failure_unbound_id \
mqtt5_client_outbound_alias_disabled_failure_alias_set \
mqtt5_client_outbound_alias_user_failure_empty_topic \
mqtt5_client_outbound_alias_lru_failure_alias_set \
mqtt5_client_outbound_alias_user_success_a_b_ar_br \
mqtt5_client_outbound_alias_lru_success_a_b_c_br_cr_a \
rate_limiter_token_bucket_init_invalid \
rate_limiter_token_bucket_regeneration_integral \
rate_limiter_token_bucket_regeneration_fractional \
rate_limiter_token_bucket_fractional_iteration \
rate_limiter_token_bucket_large_fractional_iteration \
rate_limiter_token_bucket_real_iteration \
rate_limiter_token_bucket_reset \
mqtt_packet_pubrec \
mqtt_packet_pubrel \
mqtt_packet_pubcomp \
mqtt_packet_suback \
mqtt_packet_unsuback \
mqtt_packet_connect \
mqtt_packet_connect_will \
mqtt_packet_connect_empty_payload_will \
mqtt_packet_connect_password \
mqtt_packet_connack \
mqtt_packet_publish_qos0_dup \
mqtt_packet_publish_qos2_retain \
mqtt_packet_publish_empty_payload \
mqtt_packet_subscribe \
mqtt_packet_unsubscribe \
mqtt_packet_pingreq \
mqtt_packet_pingresp \
mqtt_packet_disconnect \
mqtt_topic_tree_match \
mqtt_topic_tree_unsubscribe \
mqtt_topic_tree_duplicate_transactions \
mqtt_topic_tree_transactions \
mqtt_topic_validation \
"

for TEST in $MQTT_TESTS
do
./aws-c-mqtt-tests $TEST >> tests.log
done

sed  -e '/OK/ s/^/PASS: / ; /FAILED/ s/^/FAIL: / ; /ERROR/ s/^/FAIL: /' tests.log

### disabled tests - when doing ptest in codebuild ptest hangs - try to reduce tests - those all do pass locally ###
# mqtt_connection_publish_QoS1_timeout
# mqtt_connection_unsub_timeout
# mqtt_connection_publish_QoS1_timeout_connection_lost_reset_time
# mqtt_connect_disconnect
# mqtt_connect_set_will_login
# mqtt_connection_interrupted
# mqtt_connection_any_publish
# mqtt_connection_timeout
# mqtt_connection_connack_timeout
# mqtt_connect_subscribe
# mqtt_connect_subscribe_fail_from_broker
# mqtt_connect_subscribe_multi
# mqtt_connect_unsubscribe
# mqtt_connect_resubscribe
# mqtt_connect_publish
# mqtt_connect_publish_payload
# mqtt_connection_offline_publish
# mqtt_connection_disconnect_while_reconnecting
# mqtt_connection_closes_while_making_requests
# mqtt_connection_resend_packets
# mqtt_connection_consistent_retry_policy
# mqtt_connection_not_resend_packets_on_healthy_connection
# mqtt_connection_destory_pending_requests
# mqtt_clean_session_not_retry
# mqtt_clean_session_discard_previous
# mqtt_clean_session_keep_next_session